<?php
$con = mysql_connect("localhost","cubeMass_f","cubeSelectNow");
if (!$con)
  {
  die('Could not connect: ' . mysql_error());
  }
mysql_select_db("cubeMass", $con);
session_start();
	
	$sql_model = "SELECT * FROM model WHERE ID = '$_POST[model_id]'";
	$result_model = mysql_query($sql_model,$con);
	$row_model = mysql_fetch_array($result_model);
	
	$start = $_POST['page']*10;
	$end = $start + 10;
	
	$sql = "SELECT * FROM discuss WHERE MODEL_ID = '$_POST[model_id]' ORDER BY rankResult DESC ";
	$result = mysql_query($sql,$con);

	echo ("<div id='comment'>");
	
	$number =0; 
	while($row = mysql_fetch_array($result))
	  {	
		if($number<$end && $number>=$start){
			$sql_user = "SELECT * FROM user WHERE ID = '$row[USER_ID]' " ;
			$result_user = mysql_query($sql_user,$con);
			$row_user = mysql_fetch_array($result_user);
			$content = urldecode($row['content']);
			
			echo "<div style='background-image:url(http://www.eizia.com/webpic/cutline_408.png); height:1px; background-repeat:no-repeat'></div>";
			
			echo "<table class='commentTable' width=408px id='".$row['ID']."' border='0' cellspacing='0' cellpadding='0'>";
			
				echo "<tr class = 'commentTableTitle' >";
					echo "<td class = 'commentUser' colspan = '6' >";
					echo "<span class='commentUserName' >".$row_user['name']."</span>:<span class='commentTime' style='color:#999;'>".$row['createTime']."</span>";
					echo "</td>";
				
				echo "<tr class = 'commentContent' width='20'>";
					echo "<td>";
					echo "</td>";
					
					echo "<td colspan = '5' width='388'>";
					echo $content;
					echo "</td>";
				echo "</tr>";
				
				echo "<tr>";
					echo "<td width ='8%'>";
					echo "</td>";
					
					echo "<td width ='22%'>";
					echo "</td>";
					
					echo "<td width ='15%'>";
					echo "</td>";
					
					echo "<td width ='15%'>";
					echo "</td>";
					
						
					echo "<td  width='20%'>";
					echo "<a href='javascript:void(0)' onClick='commentSupport(1,this.parentNode.parentNode.parentNode.parentNode.id);'>同意(".$row['supportTimes'].")</a>";
					echo "</td>";
					
					echo "<td width='20%'>";
					echo "<a href ='javascript:void(0)' onClick='commentSupport(-1,this.parentNode.parentNode.parentNode.parentNode.id);'>反对(".$row['opposeTimes'].")</a>";
					echo "</td>";
				echo "</tr>";
			echo "</table>";
			}
			$number++;
	  }
	  	
		$pageLength = ceil($row_model['commentNum']/10);
		if($pageLength>1){
			echo "<div>"; 
				if($_POST['page']>0){
					$lastPage = $_POST['page']-1;
					echo "<a href='javascript:void(0)' onclick='loadComment(document.getElementById(\"showingModel\").innerHTML,".$lastPage.")'>";
					echo "上一页";
					echo "</a>";
					}
				
				if($_POST['page']<5){
					for($i=0;$i<$pageLength && $i<9;$i++){
						if($i!=$_POST['page']){
							echo "<a href='javascript:void(0)' onclick='loadComment(document.getElementById(\"showingModel\").innerHTML,".$i.")'> ";
							echo $i+1;
							echo "</a>";
							}else{
								echo "<a id='pageRightNow'>";
								echo $i+1;
								echo "</a>";
								}
							}
						}elseif ($_POST['page'] < $pageLength-4 && $_POST['page']>=5){
							for($i=$_POST['page']-4;$i<$pageLength&& $i<$_POST['page']+5;$i++){
								if($i!=$_POST['page']){
									echo "<a href='javascript:void(0)' onclick='loadComment(document.getElementById(\"showingModel\").innerHTML,".$i.")'> ";
									echo $i+1;
									echo "</a>";
									}else{
										echo "<a id='pageRightNow'>";
										echo $i+1;
										echo "</a>";
										}
									}
							}elseif ($_POST['page']>=$pageLength-4){
								for($i=$pageLength-9;$i<$pageLength;$i++){
									if($i!=$_POST['page']){
										echo "<a href='javascript:void(0)' onclick='loadComment(document.getElementById(\"showingModel\").innerHTML,".$i.")'> ";
										echo $i+1;
										echo "</a>";
										}else{
											echo "<a id='pageRightNow'> ";
											echo $i+1;
											echo "</a>";
											}
										}
								}
					}
				
				if($_POST['page']<$row_model['commentNum']/10-1){
					$nextPage = $_POST['page']+1;
					echo "<a href='javascript:void(0)' onclick='loadComment(document.getElementById(\"showingModel\").innerHTML,".$nextPage.")'>";
					echo "下一页";
					echo "</a>";
					}
			echo "</div>";
			
		if($pageLength>1){
			echo "<a href='javascript:void(0)'>";
			echo "查看所有评论(".$row_model['commentNum'].")";
			echo "</a>";
			}
		echo "<table class ='signComment'>";
			echo"<tr>";
				echo "<td >";
				echo "在这里写下你的评论吧：";
				echo "</td>";
			echo "</tr>";
			
			echo"<tr>";
				echo "<td>";
				echo "<textarea id='commentContentArea' cols='61' rows='4'></textarea>";
				echo "</td>";
			echo "</tr>";
			
			echo "<tr>";
				echo "<td>";
				echo "<input type='button' value = '提交' onClick = \"signComment(document.getElementById('commentContentArea').value,
																	document.getElementById('showingModel').innerHTML);\"></input>";
				echo "</td>";
			echo "</tr>";
			
		echo "</table>";
	
	echo("</div>");
	mysql_close($con);
?>

